﻿using BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DTO;

namespace QLVBDQ
{
    /// <summary>
    /// Interaction logic for LapBaoCaoTon.xaml
    /// </summary>
    public partial class LapBaoCaoTon : UserControl
    {
        public LapBaoCaoTon()
        {
            InitializeComponent();
        }

        private void listSanPham_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void xuatBaoCaoTon_Click(object sender, RoutedEventArgs e)
        {
            XuatBaoCao();
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            LayNgayHienTai();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadMaBaoCao();
        }

        private void ngayXuatBaoCaoTon_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadDanhSach();
           
        }

        private void LayNgayHienTai()
        {
            this.ngayXuatBaoCaoTon.DisplayDate = DateTime.Now;
            this.ngayXuatBaoCaoTon.SelectedDate = DateTime.Now;
        }

        private void LoadMaBaoCao()
        {
            string _ma = BaoCaoTon_BUS.Instance.LayMaBaoCaoCongNo();
            if (_ma == null)
            {
                return;
            }
            this.maPhieu.Text = _ma;
        }

        public void XuatBaoCao()
        {
            if (MessageBox.Show("Bạn Muốn Xuất Báo Cáo Này?", "Thông Tin", MessageBoxButton.OK) == MessageBoxResult.OK)
            {
                BaoCaoTon_DTO _item = new BaoCaoTon_DTO(this.maPhieu.Text, (DateTime)this.ngayXuatBaoCaoTon.SelectedDate);

                if (this.listSanPham.Items.Count == 0)
                {
                    MessageBox.Show("Không có thông tin nào cần xuất báo cáo!");
                    return;
                }
                if (!BaoCaoTon_BUS.Instance.ThemBaoCaoTOn(new BaoCaoTon_DTO(this.maPhieu.Text, (DateTime)this.ngayXuatBaoCaoTon.SelectedDate)))
                {
                    MessageBox.Show("Xuất Không Thành Công!");
                    return;
                }
                for (int i = 0; i < this.listSanPham.Items.Count;i++ )
                {
                    ChiTietBCTon_DTO _temp = (ChiTietBCTon_DTO)listSanPham.Items[i];
                    _temp.MaBaoCao = this.maPhieu.Text;
                    if (!ChiTietBCTon_BUS.Instance.ThemChiTietBaoCaoTon(_temp))
                    {
                        MessageBox.Show("Xuất Không Thành Công!");
                        return;
                    }
                }
                
                MessageBox.Show("Xuất Thành Công!");
                LoadMaBaoCao();
                LayNgayHienTai();
            }

        }

        private void LoadDanhSach()
        {
            List<ChiTietBCTon_DTO> list = ChiTietBCTon_BUS.Instance.LayDanhSachSanPham(this.maPhieu.Text.ToString());
            if (list == null)
            {
                return;
            }
            this.listSanPham.Items.Clear();
            for (int i = 0; i < list.Count;i++ )
            {
                list[i].STT = i + 1;
                this.listSanPham.Items.Add(list[i]);
            }
            this.listSanPham.Items.Refresh();
        }
    }
}
